﻿// <copyright file="NewsAndLogsDao.cs" company="NTalent">
// Copyright (c) 2010, NTalent, Inc. All rights reserved
// </copyright>
// <author>卢俊坤</author>
// <author>王彦博 符茂松</author>

namespace Net.Ntalent.Action50.Core.Dao
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Dynamic;
    using Net.Ntalent.Action50.DataAccess;

    /// <summary>
    /// 对于T_NEWS_AND_LOGS实体的数据库操作实体
    /// </summary>
    /// <author>王彦博</author>
    /// <copyright>
    /// Copyright (c) 2010, NTalent, Co. Ltd. All rights reserved.
    /// </copyright>
    /// <version>1.0.0</version>  
    public class NewsAndLogsDao : IDisposable
    {
        /// <summary>
        /// 数据库操作实体类，readonly, 在构造函数初始化，无getter/setter
        /// </summary>
        private readonly Action50DataContext context;

        /// <summary>
        /// 构造方法
        /// </summary>      
        public NewsAndLogsDao()
        {
            this.context = new Action50DataContext();
        }

        /// <summary>
        /// 将传入的T_NEWS_AND_LOGS实体进行持久化.
        /// </summary>
        /// <param name="action">需要新建的T_NEWS_AND_LOGS实体</param>
        /// <returns>已持久化的T_NEWS_AND_LOGS实体</returns>
        public T_NEWS_AND_LOGS Create(T_NEWS_AND_LOGS logs)
        {
            logs.POST_DATETIME = DateTime.Now;
            this.context.T_NEWS_AND_LOGS.InsertOnSubmit(logs);
            this.context.SubmitChanges();
            return logs;
        }

        /// <summary>
        /// 根据给出的id获取T_NEWS_AND_LOGS实体.
        /// </summary>
        /// <remarks>
        /// 如获取失败，返回null
        /// </remarks>
        /// <param name="id">需要获取的T_NEWS_AND_LOGS实体的id</param>
        /// <returns>符合条件的T_NEWS_AND_LOGS实体</returns>
        public T_NEWS_AND_LOGS Retrieve(long id)
        {
            return this.context.T_NEWS_AND_LOGS.Count(w => w.Id == id) == 1 ?
                this.context.T_NEWS_AND_LOGS.Single(w => w.Id == id) : null;
        }

        /// <summary>
        /// 获取所有T_NEWS_AND_LOGS，返回结果集。
        /// </summary>
        /// <remarks>
        /// 若无任何结果，则返回空集合.
        /// </remarks>
        /// <param name="newsAndLogsType">
        /// 需要获取的T_NEWS_AND_LOGS的类型
        /// </param>
        /// <param name="pageNumber">
        /// 需要获取的信息的页码,如果是0，则获取全部.
        /// </param>
        /// <param name="pageSize">
        /// 获取的页面大小,在pageNumber为0时此值将被忽略(可以为任意值)
        /// </param>
        /// <param name="orderBy">
        /// 排序表达式。获取的结果集将按此表达式排序。如果是empty，则按照id升序排列。
        /// </param>
        /// <returns>页面内所有的T_NEWS_AND_LOGS实体</returns>
        public IList<T_NEWS_AND_LOGS> RetrieveAll(NewsAndLogsType newsAndLogsType, int pageNumber, int pageSize, string orderBy)
        {
            //若orderBy为empty,按照id升序排列
            orderBy = String.IsNullOrEmpty(orderBy) ? "Id ascending" : orderBy;
            IQueryable<T_NEWS_AND_LOGS> results = null;

            //根据所需类型和页码查找实体
            if (newsAndLogsType == NewsAndLogsType.ALL)
            {
                if (pageNumber == 0)
                {
                    results = this.context.T_NEWS_AND_LOGS.OrderBy(orderBy);
                }
                else if (pageNumber > 0 && pageSize > 0)
                {
                    results = this.context.T_NEWS_AND_LOGS.OrderBy(orderBy)
                        .Skip((pageNumber - 1) * pageSize).Take(pageSize);
                }
            }
            else
            {
                if (pageNumber == 0)
                {
                    results = this.context.T_NEWS_AND_LOGS
                        .Where(w => w.LOGS_TYPE == (int)newsAndLogsType).OrderBy(orderBy);
                }
                else if (pageNumber > 0 && pageSize > 0)
                {
                    results = this.context.T_NEWS_AND_LOGS
                        .Where(w => w.LOGS_TYPE == (int)newsAndLogsType).OrderBy(orderBy)
                        .Skip((pageNumber - 1) * pageSize).Take(pageSize);
                }
            }

            //若无结果，返回空集合
            return results == null || results.Count() == 0 ? new List<T_NEWS_AND_LOGS>() : results.ToList();
        }

        /// <summary>
        /// 根据Action的id来获取所有T_NEWS_AND_LOGS。
        /// </summary>
        /// <remarks>
        /// 若无结果，则返回空集合。 
        /// </remarks>
        /// <param name="id">
        /// Action的id
        /// </param>
        /// <param name="newsAndLogsType">
        /// 所需要的T_NEWS_AND_LOGS的类型
        /// </param>
        /// <param name="pageNumber">
        /// 需要获取的信息的页码,如果是0，则获取全部.
        /// </param>
        /// <param name="pageSize">
        /// 获取的页面大小,在pageNumber为0时此值将被忽略(可以为任意值)
        /// </param>
        /// <param name="orderBy">
        /// 排序表达式。获取的结果集将按此表达式排序。如果是empty，则按照id升序排列。
        /// </param>
        /// <returns>页面内ACTION_ID等于id的所有T_NEWS_AND_LOGS实体的集合</returns>
        public IList<T_NEWS_AND_LOGS> RetrieveByAction(long id, NewsAndLogsType newsAndLogsType, int pageNumber, int pageSize, string orderBy)
        {
            //若orderBy为empty,按照id升序排列
            orderBy = String.IsNullOrEmpty(orderBy) ? "Id ascending" : orderBy;
            IQueryable<T_NEWS_AND_LOGS> results = null;

            //根据所需类型和页码以及ACTION_ID查找实体
            if (newsAndLogsType == NewsAndLogsType.ALL)
            {
                if (pageNumber == 0)
                {
                    results = this.context.T_NEWS_AND_LOGS.Where(w => w.ACTION_ID == id).OrderBy(orderBy);
                }
                else if (pageNumber > 0 && pageSize > 0)
                {
                    results = this.context.T_NEWS_AND_LOGS.Where(w => w.ACTION_ID == id).OrderBy(orderBy)
                        .Skip((pageNumber - 1) * pageSize).Take(pageSize);
                }
            }
            else
            {
                if (pageNumber == 0)
                {
                    results = this.context.T_NEWS_AND_LOGS
                        .Where(w => w.ACTION_ID == id && w.LOGS_TYPE == (int)newsAndLogsType).OrderBy(orderBy);
                }
                else if (pageNumber > 0 && pageSize > 0)
                {
                    results = this.context.T_NEWS_AND_LOGS
                        .Where(w => w.ACTION_ID == id && w.LOGS_TYPE == (int)newsAndLogsType).OrderBy(orderBy)
                        .Skip((pageNumber - 1) * pageSize).Take(pageSize);
                }
            }
            //若无结果，返回空集合。
            return results == null || results.Count() == 0 ? new List<T_NEWS_AND_LOGS>() : results.ToList();
        }

        /// <summary>
        /// 根据传入的T_NEWS_AND_LOGS的id来删除实体
        /// </summary>
        /// <remarks>
        /// 忽略所有exception
        /// </remarks>
        /// <param name="id">欲删除的T_NEWS_AND_LOGS的ID</param>
        public void Delete(long id)
        {
            try
            {
                var entities = this.context.T_NEWS_AND_LOGS.Where(w => w.Id == id);

                //删除每个news或log对应的留言
                foreach (var entity in entities)
                {
                    //找出该news或log对应的所有留言并删除。
                    var words = this.context.T_LEAVED_WORDS.Where(w => w.NEWS_AND_LOGS_ID == entity.Id);
                    this.context.T_LEAVED_WORDS.DeleteAllOnSubmit(words);
                }
                this.context.T_NEWS_AND_LOGS.DeleteAllOnSubmit(entities);
                this.context.SubmitChanges();
            }
            catch
            {
                // 忽略所有异常
            }
        }

        /// <summary>
        /// 根据传入的action id和类型，删除对应的news或logs。忽略所有exception.
        /// </summary>
        /// <param name="id">
        /// 需要删除news或logs的action的id。
        /// </param>
        /// <param name="newsAndLogsType">
        /// 需要删除的实体类型
        /// </param>
        public void DeleteByAction(long id, NewsAndLogsType newsAndLogsType)
        {
            try
            {
                if (newsAndLogsType == NewsAndLogsType.ALL)
                {
                    var entities = this.context.T_NEWS_AND_LOGS.Where(w => w.ACTION_ID == id);

                    //删除每个news或log对应的留言
                    foreach (var entity in entities)
                    {
                        //找出该news或log对应的所有留言并删除。
                        var words = this.context.T_LEAVED_WORDS.Where(w => w.NEWS_AND_LOGS_ID == entity.Id);
                        this.context.T_LEAVED_WORDS.DeleteAllOnSubmit(words);
                    }

                    this.context.T_NEWS_AND_LOGS.DeleteAllOnSubmit(entities);
                }
                else
                {
                    var entities = this.context.T_NEWS_AND_LOGS
                        .Where(w => w.ACTION_ID == id && w.LOGS_TYPE == (int)newsAndLogsType);

                    //删除每个news或Logs对应的留言
                    foreach (var entity in entities)
                    {
                        //找出该news或log对应的所有留言并删除。
                        var words = this.context.T_LEAVED_WORDS.Where(w => w.NEWS_AND_LOGS_ID == entity.Id);
                        this.context.T_LEAVED_WORDS.DeleteAllOnSubmit(words);
                    }

                    this.context.T_NEWS_AND_LOGS.DeleteAllOnSubmit(entities);
                }
                this.context.SubmitChanges();
            }
            catch
            {
                //忽略所有异常
            }
        }

        /// <summary>
        /// 将传入的T_NEWS_AND_LOGS实体信息更新
        /// </summary>
        /// <remarks>
        /// 更新失败则返回null
        /// </remarks>
        /// <param name="logs">
        /// 欲更新的实体
        /// </param>
        /// <returns>
        /// 已更新的实体
        /// </returns>
        public T_NEWS_AND_LOGS Update(T_NEWS_AND_LOGS logs)
        {
            if (this.context.T_NEWS_AND_LOGS.Count(w => w.Id == logs.Id) == 1)
            {
                // 1 获取数据库中的实体
                var entity = this.context.T_NEWS_AND_LOGS.Single(w => w.Id == logs.Id);

                // 2 将传入的logs实体中的所有字段赋值到entity

                entity.ACTION_ID = logs.ACTION_ID;
                entity.LOGS_TYPE = logs.LOGS_TYPE;
                entity.POST_CONTENT = logs.POST_CONTENT;
                entity.POST_DATETIME = logs.POST_DATETIME;
                entity.POST_TITLE = logs.POST_TITLE;
                entity.POST_USER_IP = logs.POST_USER_IP;

                // 3 执行更新
                this.context.SubmitChanges();

                // 4 返回更新后的实体
                return entity;
            }
            //更新失败返回null
            return null;
        }

        //暂不执行任何操作
        public void Dispose()
        {
        }
    }
}
